<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'member_num desc,agenttime desc',
		
	);
	$list=$this->get_list('member',$parameter,$total);
	$feeparams=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$feeparams['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}elseif($_RQ['timetype']){
		if($_RQ['timetype']==1){
			$feeparams['createtime >']=strtotime(date('Y').'-01-01');
		}elseif($_RQ['timetype']==2){
			$feeparams['createtime >']=strtotime(date('Y-m').'-01');
		}elseif($_RQ['timetype']==3){
			$feeparams['createtime >']=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  
		}
		$return['params']['timetype']=$_RQ['timetype'];
	}
	if($_RQ['goodsid']){
		$return['params']['goodsid']=$_RQ['goodsid'];
		$goodsparams=$feeparams;
	}
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$downmids=$this->getall('member',array('agent_id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
			if($downmids){
				$downmids=array_keys($downmids);
			}else{
				$downmids=-1;
			}
			if($_RQ['goodsid']){
				$item['goodstitle']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['goodsid']),'title');
				$goodsparams['goodsid']=$_RQ['goodsid'];
				$goodsparams['mid']=$downmids;
				$item['goodsnum']=pdo_getsum('jy_weishop_order_goods',$goodsparams,'total');
				$orderids=pdo_getall('jy_weishop_order_goods',array('goodsid'=>$_RQ['goodsid'],'mid'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'orderid','orderid');
				if($orderids){
					$orderids=array_keys($orderids);
				}else{
					$orderids=-1;
				}
				$ordersns=pdo_getall('jy_weishop_order',array('id'=>$orderids,'uniacid'=>$_SESSION['uniacid']),'ordersn','ordersn');
				if($ordersns){
					$ordersns=array_keys($ordersns);
				}else{
					$ordersns=-1;
				}
				$feeparams['ordersn']=$ordersns;
			}
			$feeparams['mid']=$item['mid'];
			$item['money']=pdo_getsum('jy_quanfee_commission',$feeparams,'money');
			$feeparams['mid']=$downmids;
			$item['order_price']=pdo_getsum('jy_quanfee_order',$feeparams,'order_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：根据条件搜索后，再导出表即可！</red>','key'=>'mid','edit'=>false,'del_one'=>true,'del_message'=>'确定删除分商商吗？会同时解除其下所有上下级关系哦！','list'=>array(
			array('name'=>'mid','title'=>'分红商MID','type'=>'string'),
			array('name'=>'member','title'=>'分红商','type'=>'member'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'level_id','title'=>'分红商等级','type'=>'select','options'=>'levels'),
			array('name'=>'order_price','title'=>'销售额','type'=>'string'),
			array('name'=>'money','title'=>'佣金额','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择分红商'),
			array('type'=>'choose','width'=>15,'action'=>'jy_weishop/goods.get_goods/','name'=>'goodsid','title'=>'选择商品'),
			array('name'=>'timetype','width'=>15,'title'=>'选择时间','type'=>'radio','options'=>'timetypes'),
			array('name'=>'createtime','width'=>15,'title'=>'业绩时间','type'=>'time_range'),
		),
	);
	if($_RQ['goodsid']){
		$htmls['list']['list'][]=array('name'=>'goodstitle','title'=>'商品名称','type'=>'string');
		$htmls['list']['list'][]=array('name'=>'goodsnum','title'=>'商品总数量','type'=>'string');
	}
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['timetypes']=array('1'=>'本年','2'=>'本月','3'=>'本周');
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$list=pdo_getall('jy_quanfee_member',$params,'','','member_num desc,agenttime desc');
	$feeparams=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$feeparams['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}elseif($_RQ['timetype']){
		if($_RQ['timetype']==1){
			$feeparams['createtime >']=strtotime(date('Y').'-01-01');
		}elseif($_RQ['timetype']==2){
			$feeparams['createtime >']=strtotime(date('Y-m').'-01');
		}elseif($_RQ['timetype']==3){
			$feeparams['createtime >']=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  
		}
		$return['params']['timetype']=$_RQ['timetype'];
	}
	if($_RQ['goodsid']){
		$return['params']['goodsid']=$_RQ['goodsid'];
		$goodsparams=$feeparams;
	}
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'默认等级';
			$item['agenttime']=date('Y-m-d H:m:s',$item['agenttime']);
			$downmids=$this->getall('member',array('agent_id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
			if($downmids){
				$downmids=array_keys($downmids);
			}else{
				$downmids=-1;
			}
			if($_RQ['goodsid']){
				$item['goodstitle']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['goodsid']),'title');
				$goodsparams['goodsid']=$_RQ['goodsid'];
				$goodsparams['mid']=$downmids;
				$item['goodsnum']=pdo_getsum('jy_weishop_order_goods',$goodsparams,'total');
				$orderids=pdo_getall('jy_weishop_order_goods',array('goodsid'=>$_RQ['goodsid'],'mid'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'orderid','orderid');
				if($orderids){
					$orderids=array_keys($orderids);
				}else{
					$orderids=-1;
				}
				$ordersns=pdo_getall('jy_weishop_order',array('id'=>$orderids,'uniacid'=>$_SESSION['uniacid']),'ordersn','ordersn');
				if($ordersns){
					$ordersns=array_keys($ordersns);
				}else{
					$ordersns=-1;
				}
				$feeparams['ordersn']=$ordersns;
			}
			$feeparams['mid']=$item['mid'];
			$item['money']=pdo_getsum('jy_quanfee_commission',$feeparams,'money');
			$feeparams['mid']=$downmids;
			$item['order_price']=pdo_getsum('jy_quanfee_order',$feeparams,'order_price');
		}
	}
	$header=array(
		'mid'=>'分红商MID',
		'name'=>'分红商名称',
		'mobile'=>'分红商电话',
		'from_os'=>'来源',
		'level_title'=>'分红商等级',
		'order_price'=>'销售额',
		'money'=>'产生佣金',
	);
	if($_RQ['goodsid']){
		$header['goodstitle']='商品名称';
		$header['goodsnum']='商品数量';
	}
	cfc('tools')->export2excel($header,$list,'分红商业绩表');
}
if($_RQ['op']=='export2'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>intval($_RQ['mid']));
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_quanfee_member',$params);
	$agent_status=$this->agent_status;
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'默认等级';
			$item['stop']=$agent_status[$item['stop']];
			$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
		}
	}
	$header=array(
		'mid'=>'分红商MID',
		'name'=>'分红商名称',
		'mobile'=>'分红商电话',
		'agentname'=>'推荐人',
		'from_os'=>'来源',
		'level_title'=>'分红商等级',
		'createtime'=>'创建时间',
		'stop'=>'分红商状态',
		'member_num'=>'下线数',
		'agent_num'=>'下级分红商数',
		'order_num'=>'推广订单总金额',
		'order_price'=>'推广订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'分红商');
}
if($_RQ['op']=='member'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>intval($_RQ['mid']));
	$return['params']['mid']=$_RQ['mid'];
	//是否分红商
	if(isset($_RQ['is_agent'])){
		$params['is_agent']=intval($_RQ['is_agent']);
		$return['params']['is_agent']=$_RQ['is_agent'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			//$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'分红商','type'=>'member'),
			//array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'level_id','title'=>'分红商等级','type'=>'select','options'=>'levels'),
			array('name'=>'agenttime','title'=>'成为分红商时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'stop','title'=>'分红商状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'member_num','title'=>'下线数','type'=>'string'),
			array('name'=>'agent_num','title'=>'下级分红商数','type'=>'string'),
			array('name'=>'order_num','title'=>'推广订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'推广订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'恢复','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'detail','title'=>'分红商信息'),
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='stop'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->stop_agent($mid);
	exi('禁用成功！');
}
if($_RQ['op']=='start'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->start_agent($mid);
	exi('恢复成功！');
}
if($_RQ['op']=='setleve'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$update=array(
			'level_id'=>intval($post['level_id']),
			't_time'=>strtotime($post['t_time']),
		);
		pdo_update('jy_quanfee_member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		//上级获得抽奖机会
		$gamepost=array(
			'type'=>8,
			'level_id'=>$post['level_id']
		);
		if($post['agent_id']){
			model('jy_weishop/game')->up_addgame($gamepost,$post['agent_id']);
		}
		exi('设置成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_quanfee_member',array('mid'=>$mid),array('agent_id','mid','level_id','t_time'));
	$item['t_time'] =$item['t_time']?date('Y/m/d H:i',$item['t_time']):'';
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置等级',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'等级','name'=>'level_id','options'=>'levels'),
			array('type'=>'time','title'=>'有效期','name'=>'t_time','help'=>'默认等级设置有效期无效！'),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='setarea'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$area_code=$this->model('model')->get_area_code($post['pcd']);
		$update=array(
			'area_code'=>$area_code,
			'area_address'=>$post['area_address'],
			'area_key'=>$post['area_key'],
			'pcd'=>ijson_encode($post['pcd']),
			'lat_lng'=>ijson_encode($post['lat_lng']),
		);
		pdo_update('jy_quanfee_member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		exi('设置成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_quanfee_member',array('mid'=>$mid));
	$item['lat_lng']=json_decode($item['lat_lng'],true);
	$item['pcd']=json_decode($item['pcd'],true);
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置社区代理',
	);
	$htmls['edit']=array(
			array('type'=>'pcd','title'=>'代理区域','name'=>'pcd'),
			array('type'=>'lat_lng','title'=>'代理点坐标','name'=>'lat_lng'),
			array('type'=>'string','title'=>'代理点详细地址','name'=>'area_address'),
			array('type'=>'string','title'=>'代理点社区匹配词','name'=>'area_key','help'=>'有时候很多会员的下单地址是不规范的，代理区域无法具体匹配订单地址时。可以设置匹配关键词，用|隔开。比如：爱莲小区|爱莲村'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='detail'){
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_quanfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	$item['member']=cfc('core')->member_show($item['mid']);
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['realname']?:$item['member']['wechat'];
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'分红商其他信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='qc'){
	$path=ROOT_AT .'oss/'.$_SESSION['uniacid'].'/images/jy_quanfee/';
	$handle = scandir($path);
	foreach($handle as $val){
    	//排除目录中的.和..
    	if($val !="." && $val !=".."){
    		//如果是文件直接删除
    		unlink($path.$val);
		}
   }
   $like='oss/' . $_SESSION['uniacid'].'/images/jy_quanfee/';
   pdo_delete('core_attachment',array('attachment like'=>'%'.trim($like).'%','uniacid'=>$_SESSION['uniacid']));
   exi('清除成功','','list');
}
if($_RQ['op']=='del'){
	$mid=intval($_RQ['mid']);
	if($mid){
		pdo_delete('jy_quanfee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		pdo_update('jy_quanfee_member',array('agent_id'=>0),array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid));
		pdo_delete('jy_quanfee_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $mid){
			$mid=intval($mid);
			if($mid){
				pdo_delete('jy_quanfee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
				pdo_update('jy_quanfee_member',array('agent_id'=>0),array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid));
				pdo_delete('jy_quanfee_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>